import { Injectable } from "@angular/core";
import { HttpClient, HttpParams } from "@angular/common/http";
import { Observable } from "rxjs";
import { BaseHttpService } from "../../../app/shared/services/baseHttp.service";
import { StorageService } from "../../../app/shared/services/storage.service";
import { GovernanceInsightModel, GovernancePdfType } from "./models/governanceInsight.model";
import { GovernanceMetadataModel } from "./models/governanceMetadata.model";
import { GovernanceProfileModel } from "./models/governanceProfile.model";

@Injectable()
export class GovernanceReportsService extends BaseHttpService {
    constructor(private http: HttpClient) {
        super();
    }

    public getCompanies(term: string, skip: number = 0, take: number = 20): Observable<any[]> {
        let userInfo = StorageService.get().userInfo;
        let params: HttpParams = new HttpParams();
        params = params.set('userId', userInfo.userId)
            .set('accountId', userInfo.accountId)
            .set('term', term)
            .set('skip', skip.toString())
            .set('take', take.toString())
            .set('fromAdmin', true.toString());

        return this.http.get<any[]>(this.serverUrl + 'companyserv/api/v1/companies', { params: params });
    }

    public getGovernanceReport(companyId: string): Observable<GovernanceProfileModel> {
        return this.http.get<GovernanceProfileModel>(this.serverUrl + 'governanceserv/api/v1/company/' + companyId);
    }

    public setGovernanceReport(companyId: string, fileName: string, type: GovernancePdfType): Observable<boolean> {
        let body = {
            companyId: companyId,
            fileName: fileName,
            governancePdfType: type
        };
        return this.http.post<boolean>(this.serverUrl + 'governanceserv/api/v1/governanceadminpdfmetadata/', body);
    }

    public removeGovernanceReport(companyId: string, type: GovernancePdfType) {
        let params: HttpParams = new HttpParams();
        params = params.set('companyId', companyId)
            .set('governancePdfType', type.toString());
        return this.http.delete<void>(this.serverUrl + 'governanceserv/api/v1/governanceadminpdfmetadata', { params: params })
    }

    public getGovernanceMetadata(companyId: string): Observable<GovernanceMetadataModel[]> {
        return this.http.get<GovernanceMetadataModel[]>(this.serverUrl + 'governanceserv/api/v1/governanceadminpdfmetadata/' + companyId);
    }

    public getGovernanceInsights(companyId: string): Observable<GovernanceInsightModel> {
        return this.http.get<GovernanceInsightModel>(this.serverUrl + 'governanceserv/api/v1/governanceadmin/' + companyId);
    }

    public setGovernanceInsights(insight: any): Observable<GovernanceInsightModel> {
        return this.http.post<GovernanceInsightModel>(this.serverUrl + 'governanceserv/api/v1/governanceadmin/', insight);
    }

    public removeGovernanceInsights(companyId: string, fileId: string): Observable<void> {
        let params: HttpParams = new HttpParams();
        params = params.set('companyId', companyId)
            .set('fileId', fileId);
        return this.http.delete<void>(this.serverUrl + 'governanceserv/api/v1/governanceadminpdfmetadata', { params: params })
    }
}